﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Inaction.Linq.Clause
{
    class ConstantClause : ValueClause
    {
        public ConstantClause(LinqRoot root, ConstantExpression e)
            : base(root, null, e)
        {
            Type = e.Type;
            if (Type == typeof(bool))
            {
                Left = new BooleanClause(root, (bool)e.Value);
            }
            else if (e.Value == null)
            {
                Left = new NullClause(root);
            }
            else
            {
                ParaValue = root.AddPara(e.Value);
            }
        }

        public ConstantClause(LinqRoot root, object value)
            : base(root)
        {
            if (value == null)
            {
                Left = new NullClause(root);
            }
            else if (value.GetType() == typeof(bool))
            {
                Left = new BooleanClause(root, (bool)value);
            }
            else if(value is System.Collections.IList)
            {
                foreach( var item in value as System.Collections.IList)
                {
                    this.ParaValue = root.AddPara(item);
                }
            }
            else if(value.GetType().IsValueType)
            {
                this.ParaValue = root.AddPara(value);
            }
            else
            {
                this.ParaValue = root.AddPara(value);
            }
        }

        public override string GetClauseSql()
        {
            if (Left != null)
            {
                return Left.GetClauseSql();
            }
            return base.GetClauseSql();
        }
    }
}
